package oldcode;

import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractMonitor{

	public static Logger logger = LoggerFactory.getLogger(AbstractMonitor.class);  
	
	private boolean isonline = false;
	
	abstract public ServiceInstance<String> getServiceInstance();
	
	abstract public boolean isAvailable() ;
	
	public void runOnce(ServiceDiscovery<String> serviceDiscovery) throws Exception
	{
		ServiceInstance<String> ins = getServiceInstance();
		
		logger.debug("detect service start: " + ins.getName());
		
        boolean available = isAvailable();
        
        if(available && !isonline)
        {
        	isonline = true;
        	
        	serviceDiscovery.registerService(ins);
        	
        	logger.info("service state change to [online]: " + ins.toString());
        	
        }
        else if(!available && isonline)
        {
        	isonline = false;
        	
        	serviceDiscovery.unregisterService(ins);
        	
        	logger.info("service state change to [offline]: " + ins.toString());
        }
        
        logger.debug("detect service finish!");
	}

	
	public static void main(String[] args) {

	}
}
